package com.springboot.others.controller;

import com.springboot.others.common.Result;
import com.springboot.others.entity.Customer;
import com.springboot.others.entity.dto.PageCustomerDTO;
import com.springboot.others.entity.vo.ResultPage;
import com.springboot.others.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
@Slf4j
public class CustomerController {


    @Autowired
    private CustomerService customerService;

    @GetMapping("/{id}")
    public Result<Customer> getById(@PathVariable Integer id){
        Customer customer = customerService.findById(id);
        return Result.success(customer);
    }
    @GetMapping
    public Result<List<Customer>> getAll(){
        return Result.success(customerService.findAll());
    }

    @GetMapping("/page")
    public Result<ResultPage> getPage(PageCustomerDTO pageCustomerDTO){
        log.info("pageCustomerDTO:{}",pageCustomerDTO);
        return Result.success(customerService.getPageByCondition(pageCustomerDTO));
    }

    @PutMapping("/update")
    public Result Update(@RequestBody Customer customer){
        customerService.update(customer);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        customerService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/add")
    public Result save(@RequestBody Customer customer){
        log.info("customer:{}",customer);
        customerService.save(customer);
        return Result.success();
    }
}
